class_name Idel
extends NodeState

@export var player: Player
@export var animated_sprite_2d: AnimatedSprite2D
@export var ray:RayCast2D

var current_npc:Villager = null

func _on_process(_delta : float) -> void:
	pass


func _on_physics_process(_delta : float) -> void:
	#print("idle:_on_physics_process,player position",player.position)
	if player.player_direction == Vector2.UP:
		animated_sprite_2d.play("idle_up")
	elif player.player_direction == Vector2.RIGHT:
		animated_sprite_2d.play("idle_right")
	elif player.player_direction == Vector2.DOWN:
		animated_sprite_2d.play("idle_down")
	elif player.player_direction == Vector2.LEFT:
		animated_sprite_2d.play("idle_left")
	else:
		animated_sprite_2d.play("idle_down")
	if ray.is_colliding(): 
	
		#draw_line(Vector2.ZERO, ray.cast_to.rotated(ray.rotation),  Color.red) 
		var collider = ray.get_collider() 
		if collider is Villager : 
			current_npc = collider
			if(GameInputEvents.a_pressed()):
				print_debug("对话：",current_npc.dialogue.pick_random()) 	


func _on_next_transitions() -> void:
	# get direction
	GameInputEvents.movement_input()
	
	if GameInputEvents.is_movement_input():
		print("transition to move")
		transition.emit("move")
	
	if player.current_tool == DataTypes.Tools.AxeWood && GameInputEvents.use_tool():
		transition.emit("Chopping")
	
	if player.current_tool == DataTypes.Tools.TillGround && GameInputEvents.use_tool():
		transition.emit("Tilling")
	
	if player.current_tool == DataTypes.Tools.WaterCrops && GameInputEvents.use_tool():
		transition.emit("Watering")


func _on_enter() -> void:
	pass


func _on_exit() -> void:
	animated_sprite_2d.stop()

func on_start_dialogue()->void:
	print("对话：",current_npc.dialogue.pick_random())
